package com.happystreet.engine
{
	import com.happystreet.GlobalGameConstants;
	import com.happystreet.config.GameConfig;
	import com.happystreet.config.ItemsConfig;
	import com.happystreet.info.ItemInfo;
	import com.happystreet.ui.CoreButton;
	import com.happystreet.ui.ItemBox;
	import com.happystreet.utils.CustomUtils;
	
	import flash.display.MovieClip;
	import flash.geom.Point;

	/**
	 * 商店单元
	 * @author Liseen
	 * 
	 */	
	public class ShopItem extends CoreObject
	{
		public static const SHOP_VIEW_AREA:String = "shopViewArea";
		public static const CUSTOMIZATION_TOOL_AREA:String = "customToolArea";
		public static const NO_SHOP_CONTENT_INDEX:int = -1;
		private static const AREA_MARGIN:int = 8;
		
		public var contentHeight:int;
		private var placementRule:String;
		public var isCash:Boolean;
		private var dragAreaMaxY:Number;
		public var referenceFloor:int;
		public var saleCoins:int;
		public var wallMaterialId:int;
		public var itemId:int;
		public var isFlipable:Boolean;
		private var wallMaterialPreviewId:int;
		public var isNew:Boolean;
		public var priceCash:int;
		private var remainingUserCoins:uint;
		public var saleCash:int;
		public var flipState:int = 0;
		public var uniqueID:int;
		public var isFromWarehouse:Boolean;
		private var remainingUserCash:int;
		private var initPlacement:Point;
		private var hasMoved:Boolean;
		private var dragOutsideMinY:int;
		public var priceCoins:int;
		public var symbolName:String;
		private var dragAreaMinX:Number;
		private var dragAreaMinY:Number;
		public var notPlacedInShop:Boolean;
		public var indexGroup:int;
		private var bottomPanelScale:Number;
		private var dragOffsetY:Number;
		private var dragOffsetX:Number;
		public var itemInfo:ItemInfo;
		private var dragOutsideMargeOut:int;
		public var itemContent:CoreObject;
		private var referenceShop:CoreShop;
		public var isReallyInWarehouse:Boolean;
		public var itemType:int;
		private var dragAreaMaxX:Number;
		public var movingArea:String;
		private var flipButton:CoreButton;
		public function ShopItem(className:String=null, inWareHouse:Boolean=true)
		{
			super(className);
			isReallyInWarehouse = inWareHouse;
		}
		
		/**
		 *设置配置 
		 * @param info
		 * 
		 */		
		public function setItemConfig(info:ItemInfo):void
		{
			this.itemInfo = info;
			if(!this.itemInfo||!this.itemInfo.hasOwnProperty("type")){
				return;
			}
			
			this.itemType = info.type;
			this.indexGroup = info.groupIndex;
			this.itemId = info.id;
			name = info.name;
			this.isCash = info.isCash;
			this.isNew = info.isNew;
			this.priceCoins = info.priceInCoins;
			this.saleCoins = info.salesPriceInCoins;
			this.priceCash = info.priceInCash;
			this.saleCash = info.salesPriceInCash;
			this.symbolName = info.symbolName;
			switch (this.itemType)
			{
				case GlobalGameConstants.DOOR_TYPE:
					this.isFlipable = true;
					this.placementRule = GlobalGameConstants.ITEM_DOOR_PLACEMENT_RULE;
					break;
				case GlobalGameConstants.OUTSIDE_DECORATION_TYPE:
					this.isFlipable = true;
					this.placementRule = GlobalGameConstants.ITEM_OUTSIDE_PLACEMENT_RULE;
					break;
				case GlobalGameConstants.ROOF_TYPE:
					this.placementRule = GlobalGameConstants.ITEM_ROOF_PLACEMENT_RULE;
					break;
				case GlobalGameConstants.WALL_MATERIAL_TYPE:
					this.alpha = 0.5;
					this.placementRule = GlobalGameConstants.ITEM_FLOOR_PLACEMENT_RULE;
					break;
				case GlobalGameConstants.WALL_SHAPE_TYPE:
					this.alpha = 0.5;
					this.placementRule = GlobalGameConstants.ITEM_FLOOR_PLACEMENT_RULE;
					break;
				case GlobalGameConstants.BANNER_TYPE:
					this.placementRule = GlobalGameConstants.ITEM_AREA_PLACEMENT_RULE;
					break;
				case GlobalGameConstants.STORE_WINDOW_TYPE:
				case GlobalGameConstants.WALL_DECORATION_TYPE:
				case GlobalGameConstants.WINDOW_TYPE:
				default:
					this.isFlipable = true;
					this.placementRule = GlobalGameConstants.ITEM_AREA_PLACEMENT_RULE;
			}
			this.itemContent = new CoreObject(this.symbolName);
			this.contentHeight = Math.floor(this.itemContent.height);
			this.add(this.itemContent);
			this.bottomPanelScale = CustomUtils.getMaxInscribeScale(this.itemContent, ItemBox.BOX_CONTENT_WIDTH);
		}
		
		public function setFlipState(_arg1:int):void
		{
			this.flipState = _arg1;
			if (!this.itemContent)
			{
				return;
			}
			this.itemContent.flip(this.flipState);
			if (this.flipState == CoreObject.FLIP_HORIZONTAL)
			{
				this.itemContent.x = this.width;
			}
			else
			{
				if (this.flipState == CoreObject.FLIP_RESET)
				{
					this.itemContent.x = 0;
				}
			}
		}

		
		public function setShopReference(_arg1:int, _arg2:Boolean):void
		{
			var _local3:int;
			var _local4:int;
			this.referenceShop = GameConfig.shopList[_arg1];
			if (_arg2)
			{
				this.referenceShop.saveItem(this);
				this.movingArea = CUSTOMIZATION_TOOL_AREA;
				this.itemContent.stopContent();
				switch (this.itemType)
				{
					case GlobalGameConstants.WALL_SHAPE_TYPE:
					case GlobalGameConstants.WALL_MATERIAL_TYPE:
						break;
					case GlobalGameConstants.ROOF_TYPE:
						_local3 = this.referenceShop.workingShopLayoutContent[this.referenceShop.floorList.length][1];
						_local4 = ItemsConfig.getItemInfo(_local3).priceInCoins;
						this.referenceShop.beautyBar.previewBeauty((this.priceCoins - _local4));
						break;
					default:
						this.referenceShop.beautyBar.previewBeauty(this.priceCoins);
				}
			}
			else
			{
				this.movingArea = SHOP_VIEW_AREA;
			}
			this.setDragArea();
		}
		
		public function setDragArea(_arg1:Boolean=false):void
		{
			var _local2:int;
			if (this.itemType != GlobalGameConstants.DOOR_TYPE)
			{
				_local2 = 100;
			}
			if (this.referenceShop.infoArea)
			{
				this.dragAreaMinX = ((-((this.referenceShop.infoArea.width / 2)) - this.width) - _local2);
				this.dragAreaMinY = -((this.referenceShop.infoArea.height + this.contentHeight));
				this.dragAreaMaxX = ((this.referenceShop.infoArea.width / 2) + _local2);
				this.dragAreaMaxY = -(this.contentHeight);
			}
			this.dragOutsideMargeOut = 3;
			this.dragOutsideMinY = this.getAreaMaxHeight();
			this.y = Math.max(this.y, this.dragOutsideMinY);
		}
		
		public function getAreaMaxHeight(_arg1:Boolean=false):int
		{
			var _local2:int = Math.floor((GameConfig.STREET_HORIZON_HEIGHT - (GameConfig.STREET_HORIZON_HEIGHT / GameConfig.CAMERA_SHOP_VIEW_ZOOM_FACTOR)));
			if (_arg1)
			{
				return (Math.floor(((GameConfig.SHOP_AREA_GROUND - _local2) - AREA_MARGIN)));
			}
			return (-(Math.floor((((GameConfig.SHOP_AREA_GROUND - _local2) + this.height) - AREA_MARGIN))));
		}
	}
}